package com.shaozi.nc.quartz.manage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shaozi.common.schedule.DyncQuartzManage;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.consts.SCConsts;
import com.shaozi.nc.consts.SCConsts.QuartzKey;
import com.shaozi.nc.data.hibVO.ReportVO;
import com.shaozi.nc.hibservice.ReportService;
import com.shaozi.nc.quartz.base.Consts;
import com.shaozi.nc.quartz.workreport.ReportJobExecute;
import com.shaozi.utils.date.DateHelper;
import com.shaozi.utils.date.bean.DateBean;

@Service
public class ReportManage {

	public final static String ALARM = "report.alarm";

	private Logger logger = Logger.getLogger(ReportManage.class);

	@Autowired
	private ReportService reportService;

	@Autowired
	private DyncQuartzManage dyncQuartzMgr;

	public void scheduleByConfig(String companyId) throws SzException, SchedulerException {
		this.scheduleByConfig(companyId, false);
	}

	public void scheduleByConfig(String companyId, boolean isNoNotice) throws SzException, SchedulerException {
		DateBean dateBean = DateHelper.getDateBean();
		String[] timeTypes = new String[] { "Start", "End" };
		int alarmType = 0;
		for (String tt : timeTypes) {
			alarmType++;
			List<ReportVO> reports = reportService.listReportsByDate(companyId, dateBean, tt);
			if (reports.isEmpty()) {
				logger.info("companyId=" + companyId + " load report empty;date=" + dateBean + " timetype=" + tt);
				continue;
			}
			int cnt = 0;
			for (ReportVO report : reports) {
				long alarmTime = 0;
				if (alarmType == SCConsts.ReportAlarmType.AlarmType_Begin)
					alarmTime = report.getRemindStartTime();
				else {
					// if (alarmTime < System.currentTimeMillis())
					// continue;
					alarmTime = report.getRemindEndTime();
				}

				Map<String, String> dataMap = commonDataMap(companyId, alarmType, report, alarmTime);
				cnt++;
				dataMap.put("isNoNotice", isNoNotice + "");
				dataMap.put("createTime", System.currentTimeMillis() + "");
				addJob(companyId, report, dataMap, alarmType, alarmTime);
			}
			logger.info("companyId=" + companyId + " load report " + cnt + ";date=" + dateBean + " timetype=" + tt);
		}
	}

	public Map<String, String> commonDataMap(String companyId, int alarmType, ReportVO report, long alarmTime) {
		Map<String, String> dataMap = new HashMap<>();
		dataMap.put("companyId", companyId);
		dataMap.put("alarmType", String.valueOf(alarmType));
		dataMap.put("type", String.valueOf(report.getType()));
		dataMap.put("reportId", String.valueOf(report.getId()));
		dataMap.put("uid", String.valueOf(report.getUid()));
		dataMap.put(Consts.DataKey_StartAt, String.valueOf(alarmTime));
		return dataMap;
	}

	private void addJob(String companyId, ReportVO report, Map<String, String> dataMap, int alarmType, long startAt)
			throws SchedulerException {
		String jobName = String.format("%s.%s.%d.%d.%d", companyId, ALARM, report.getUid(), report.getId(), alarmType);
		String triggerName = String.format("trigger.%s", jobName);
		String groupKey = QuartzKey.ReportAlarmGroupKey;
		if (dyncQuartzMgr.existTrigger(groupKey, triggerName)) {
			logger.warn("exist trigger:" + triggerName);
			return;
		}
		dyncQuartzMgr.addJobWithOnce(groupKey, jobName, ReportJobExecute.class, groupKey, triggerName, startAt,
				dataMap);

	}
}
